{% extends 'encyclopedia/base.html' %}

{% block title %}相机配件列表 - 相机百科{% endblock %}

{% block content %}
<!-- 页面标题区域 -->
<div class="mb-6 text-center">
    <h1 class="text-[clamp(1.5rem,3vw,2.5rem)] font-bold text-gray-800 mb-2">
        <i class="fa fa-puzzle-piece text-primary mr-2"></i>相机配件列表
    </h1>
    <p class="text-gray-600">发现并浏览各种相机配件，提升您的摄影体验</p>
</div>

<!-- 搜索和过滤区域 -->
<div class="bg-white rounded-xl shadow-sm p-5 mb-6">
    <div class="flex flex-col md:flex-row gap-4 items-center">
        <!-- 搜索框 -->
        <div class="relative flex-grow w-full md:w-auto">
            <form method="get" action="" class="relative">
                <div class="absolute inset-y-0 left-0 flex items-center pl-3 pointer-events-none">
                    <i class="fa fa-search text-gray-400"></i>
                </div>
                <input 
                    type="text" 
                    name="search" 
                    placeholder="搜索配件名称、品牌或描述..." 
                    value="{{ request.GET.search }}" 
                    class="pl-10 pr-4 py-2 w-full rounded-lg border border-gray-200 focus:ring-2 focus:ring-primary/50 focus:border-primary outline-none transition-all"
                >
                <button type="submit" class="absolute right-2 top-1/2 transform -translate-y-1/2 bg-primary text-white p-2 rounded-lg hover:bg-primary/90 transition-colors">
                    <i class="fa fa-search"></i>
                </button>
            </form>
        </div>
        
        <!-- 分类筛选 -->
        <div class="w-full md:w-auto">
            <div class="inline-flex items-center rounded-lg bg-gray-100 p-1">
                <a 
                    href="{% url 'encyclopedia:accessory_list' %}" 
                    class="px-4 py-2 rounded-lg text-sm font-medium transition-colors {% if not request.GET.category %}bg-white shadow-sm text-primary{% else %}text-gray-700 hover:bg-gray-200{% endif %}"
                >
                    全部
                </a>
                {% for category in categories %}
                <a 
                    href="?category={{ category.id }}{% if request.GET.search %}&search={{ request.GET.search }}{% endif %}" 
                    class="px-4 py-2 rounded-lg text-sm font-medium transition-colors {% if request.GET.category == category.id|stringformat:'s' %}bg-white shadow-sm text-primary{% else %}text-gray-700 hover:bg-gray-200{% endif %}"
                >
                    {{ category.name }}
                </a>
                {% endfor %}
            </div>
        </div>
    </div>
    
    <!-- 操作按钮 -->
    <div class="mt-4 flex flex-wrap gap-2 justify-center md:justify-end">
        <a href="{% url 'encyclopedia:accessory_category_list' %}" class="btn btn-outline-primary rounded-lg shadow-sm">
            <i class="fa fa-th-large mr-2"></i>配件分类管理
        </a>
        <a href="{% url 'encyclopedia:accessory_add' %}" class="btn btn-success rounded-lg shadow-sm">
            <i class="fa fa-plus mr-2"></i>添加配件
        </a>
    </div>
</div>

<!-- 配件列表 -->
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
    {% if accessories %}
        {% for accessory in accessories %}
        <div class="bg-white rounded-xl shadow-sm overflow-hidden transition-all duration-300 hover:shadow-md hover:-translate-y-1 accessory-card">
            <!-- 卡片头部 -->
            <div class="bg-gradient-to-r from-primary/90 to-primary p-4 text-white">
                <h3 class="font-bold text-lg mb-1">{{ accessory.title }}</h3>
                <div class="flex items-center gap-3 text-sm">
                    <span class="inline-flex items-center bg-white/20 rounded-full px-2 py-1">
                        <i class="fa fa-building-o mr-1"></i>{{ accessory.brand }}
                    </span>
                    <span class="inline-flex items-center bg-white/20 rounded-full px-2 py-1">
                        <i class="fa fa-tag mr-1"></i>{{ accessory.category.name }}
                    </span>
                </div>
            </div>
            
            <!-- 卡片内容 -->
            <div class="p-4">
                {% if accessory.price_range %}
                <div class="bg-success/10 text-success font-medium rounded-lg p-2 mb-3 flex items-center justify-between">
                    <span><i class="fa fa-yen mr-1"></i>价格区间</span>
                    <span>{{ accessory.price_range }}</span>
                </div>
                {% endif %}
                
                <p class="text-gray-700 mb-4 line-clamp-3">
                    {{ accessory.content|truncatechars:120 }}
                </p>
                
                <!-- 操作按钮 -->
                <div class="flex flex-wrap gap-2">
                    <a href="{% url 'encyclopedia:accessory_detail' accessory.pk %}" class="btn btn-primary btn-sm rounded-lg shadow-sm flex-1">
                        <i class="fa fa-eye mr-1"></i> 查看详情
                    </a>
                    <a href="{% url 'encyclopedia:accessory_edit' accessory.pk %}" class="btn btn-secondary btn-sm rounded-lg shadow-sm flex-1">
                        <i class="fa fa-pencil mr-1"></i> 编辑
                    </a>
                    <a href="{% url 'encyclopedia:accessory_delete' accessory.pk %}" class="btn btn-danger btn-sm rounded-lg shadow-sm">
                        <i class="fa fa-trash mr-1"></i>
                    </a>
                </div>
            </div>
            
            <!-- 卡片底部 -->
            <div class="bg-gray-50 p-3 text-xs text-gray-500 flex justify-between">
                <span>创建: {{ accessory.created_at|date:"Y-m-d" }}</span>
                {% if accessory.updated_at != accessory.created_at %}
                <span>更新: {{ accessory.updated_at|date:"Y-m-d" }}</span>
                {% endif %}
            </div>
        </div>
        {% endfor %}
    {% else %}
    <!-- 空状态 -->
    <div class="col-12">
        <div class="bg-white rounded-xl shadow-sm p-8 text-center">
            <div class="text-6xl mb-4 text-gray-300">
                <i class="fa fa-puzzle-piece"></i>
            </div>
            <h3 class="text-xl font-semibold text-gray-800 mb-2">暂无配件数据</h3>
            <p class="text-gray-600 mb-6">当前筛选条件下没有找到配件，试试其他条件或添加新配件</p>
            <a href="{% url 'encyclopedia:accessory_add' %}" class="btn btn-success rounded-lg shadow-sm">
                <i class="fa fa-plus mr-2"></i>添加第一个配件
            </a>
        </div>
    </div>
    {% endif %}
</div>

<!-- 分页 -->
{% if is_paginated %}
<div class="mt-8 flex justify-center">
    <nav aria-label="Page navigation">
        <ul class="inline-flex items-center space-x-1">
            {% if page_obj.has_previous %}
            <li>
                <a href="?page=1{% if request.GET.category %}&category={{ request.GET.category }}{% endif %}{% if request.GET.search %}&search={{ request.GET.search }}{% endif %}" class="px-3 py-2 rounded-lg border border-gray-200 text-sm font-medium text-gray-700 hover:bg-gray-50 focus:z-20 focus:ring-2 focus:ring-primary/50 focus:border-primary transition-colors">
                    <i class="fa fa-angle-double-left"></i>
                </a>
            </li>
            <li>
                <a href="?page={{ page_obj.previous_page_number }}{% if request.GET.category %}&category={{ request.GET.category }}{% endif %}{% if request.GET.search %}&search={{ request.GET.search }}{% endif %}" class="px-3 py-2 rounded-lg border border-gray-200 text-sm font-medium text-gray-700 hover:bg-gray-50 focus:z-20 focus:ring-2 focus:ring-primary/50 focus:border-primary transition-colors">
                    <i class="fa fa-angle-left"></i>
                </a>
            </li>
            {% endif %}
            
            {% for num in page_obj.paginator.page_range %}
                {% if page_obj.number == num %}
                <li>
                    <a href="?page={{ num }}{% if request.GET.category %}&category={{ request.GET.category }}{% endif %}{% if request.GET.search %}&search={{ request.GET.search }}{% endif %}" aria-current="page" class="px-4 py-2 rounded-lg border border-primary bg-primary text-sm font-medium text-white focus:z-20 focus:ring-2 focus:ring-primary/50 focus:border-primary">
                        {{ num }}
                    </a>
                </li>
                {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                <li>
                    <a href="?page={{ num }}{% if request.GET.category %}&category={{ request.GET.category }}{% endif %}{% if request.GET.search %}&search={{ request.GET.search }}{% endif %}" class="px-4 py-2 rounded-lg border border-gray-200 text-sm font-medium text-gray-700 hover:bg-gray-50 focus:z-20 focus:ring-2 focus:ring-primary/50 focus:border-primary transition-colors">
                        {{ num }}
                    </a>
                </li>
                {% endif %}
            {% endfor %}
            
            {% if page_obj.has_next %}
            <li>
                <a href="?page={{ page_obj.next_page_number }}{% if request.GET.category %}&category={{ request.GET.category }}{% endif %}{% if request.GET.search %}&search={{ request.GET.search }}{% endif %}" class="px-3 py-2 rounded-lg border border-gray-200 text-sm font-medium text-gray-700 hover:bg-gray-50 focus:z-20 focus:ring-2 focus:ring-primary/50 focus:border-primary transition-colors">
                    <i class="fa fa-angle-right"></i>
                </a>
            </li>
            <li>
                <a href="?page={{ page_obj.paginator.num_pages }}{% if request.GET.category %}&category={{ request.GET.category }}{% endif %}{% if request.GET.search %}&search={{ request.GET.search }}{% endif %}" class="px-3 py-2 rounded-lg border border-gray-200 text-sm font-medium text-gray-700 hover:bg-gray-50 focus:z-20 focus:ring-2 focus:ring-primary/50 focus:border-primary transition-colors">
                    <i class="fa fa-angle-double-right"></i>
                </a>
            </li>
            {% endif %}
        </ul>
    </nav>
</div>
{% endif %}

<script>
    // 添加卡片加载动画
    document.addEventListener('DOMContentLoaded', function() {
        const cards = document.querySelectorAll('.accessory-card');
        cards.forEach((card, index) => {
            // 设置初始状态
            card.style.opacity = '0';
            card.style.transform = 'translateY(20px)';
            
            // 延迟显示，创造级联效果
            setTimeout(() => {
                card.style.opacity = '1';
                card.style.transform = 'translateY(0)';
            }, 100 * index);
        });
    });
</script>

<style>
    /* 自定义样式 */
    .accessory-card {
        height: 100%;
        display: flex;
        flex-direction: column;
    }
    
    .accessory-card > .p-4 {
        flex-grow: 1;
        display: flex;
        flex-direction: column;
    }
    
    .accessory-card > .p-4 > .flex {
        margin-top: auto;
    }
</style>
{% endblock %}